from flask import Flask, jsonify, request
# 导入配制文件
from config import Config
from db import db1
from flask_cors import CORS

import json

app = Flask(__name__)
# 加载配制文件
app.config.from_object(Config)
# 跨域配制
CORS(app)


@app.route('/adds', methods=['POST', 'GET'])
def add():
    if request.method == "POST":
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "insert into test values(0,'%s')" % (data['name'])
        db1.update(sql)
        return jsonify({'code': 200})
    else:
        sql1 = "select * from test"
        res = db1.find_all(sql1)
        return jsonify({'code': 200, 'list': res})


if __name__ == "__main__":
    app.run()
